﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers
{
    internal class CmsUserGroupDataMapper : CmsBaseSqlEntityDataMapper<CmsUserGroup, int>
    {
        private static Dictionary<string, Func<object, IList<SqlParameter>>> _mappersTo = new Dictionary<string, Func<object, IList<SqlParameter>>>();

        static CmsUserGroupDataMapper()
        {
            _mappersTo.Add("cms_sp_UserGroupSave", CreateMapperForUpdate);
        }

        public CmsUserGroupDataMapper(IList<string> ignoreProperties)
            : base(ignoreProperties)
        { }

        public override IList<SqlParameter> MapTo(string sp, object parameter, bool isForCreate)
        {
            Guard.ArgumentNotEmpty(sp, "sp");

            return _mappersTo.ContainsKey(sp) ?
                _mappersTo[sp](parameter) :
                base.MapTo(sp, parameter, isForCreate);
        }

        private static IList<SqlParameter> CreateMapperForUpdate(object parameter)
        {
            IList<SqlParameter> result = new List<SqlParameter>();

            var cmsUserGroup = parameter as CmsUserGroup;
            if (cmsUserGroup != null)
            {
                result.Add(new SqlParameter("Id", cmsUserGroup.Id));
                result.Add(new SqlParameter("SiteGroupId", cmsUserGroup.SiteGroupId));
                result.Add(new SqlParameter("SiteId", cmsUserGroup.SiteId));
                result.Add(new SqlParameter("GroupName", cmsUserGroup.GroupName));
                result.Add(new SqlParameter("IsEnabled", cmsUserGroup.IsEnabled));
                result.Add(new SqlParameter("IsSystem", cmsUserGroup.IsSystem));
                result.Add(new SqlParameter("LastUpdatedBy", cmsUserGroup.LastUpdatedBy));
                result.Add(new SqlParameter("DateLastUpdated", cmsUserGroup.DateLastUpdated));
            }

            return result;
        }
    }
}
